/**
 *
 */
angular
    .module('baocloud.createCloud')
    .directive('linkSelect', linkSelect)
    .controller('LinkSelectController', LinkSelectController)
    .directive('ubtnGroup', ubtnGroup)
    .factory('ubtnGroupHandle', ubtnGroupHandle)
    .controller('UbtnGroupController', UbtnGroupController)
    .directive('selectRect', selectRect)
    .factory('selectRectHandle', selectRectHandle)
    .factory('uuid', uuid)
    .controller('SelectRectController', SelectRectController)
    .controller('CreateCloudController', CreateCloudController);


function linkSelect() {
    var directive = {
        restrict: 'E',
        replace: true,
        scope: {
            datasets: '=',
            selected: '=',
        },
        templateUrl: function (element, attrs) {
            return attrs.templateUrl || 'CreateCloud/linkSelect.html';
        },
        controller: 'LinkSelectController',
        controllerAs: 'linkSelect'
    };
    return directive;
}


function LinkSelectController($scope) {
    var self = this;
    var datasets = self.datasets = $scope.datasets;
    var selected = self.selected = $scope.selected;
    self.onChange = function(code){
        console.log(code);
    }

    $scope.$watch('selected[0]',function(newVal,oldVal){
        console.log('watch.LinkSelectController-->',newVal,oldVal);
    });
}

function ubtnGroup() {
    var directive = {
        restrict: 'AE',
        replace: true,
        transclude: true,
        scope: {
            ubtns: '=',
            selected: '=',
            tabClick: '&'
        },
        templateUrl: function (element, attrs) {
            return attrs.templateUrl || 'CreateCloud/ubtnGroup.html';
        },
        link:link,
        controller: 'UbtnGroupController',
        controllerAs: 'ubtnGroup'

    };
    return directive;
}

function link(scope, element, attr, ctrl){
}

function UbtnGroupController($scope, ubtnGroupHandle) {
    var self = this;
    var ubtns = self.ubtns = $scope.ubtns;
    var selected = self.selected = $scope.selected;
    self.onTabClick = function (code) {
        selected[0] = code;
        $scope.tabClick();
        //console.log('onTabClick--->',selected);
        //ubtnGroupHandle.onTabClick(index, ubtns[index]);
    }
    $scope.$watch('selected[0]',function(newVal,oldVal){
        console.log('watch.ubtn-->',newVal,oldVal);
    });
}


function selectRect() {
    var directive = {
        restrict: 'AE',
        replace: true,
        transclude: true,
        scope: {
            index: '@',
            options: '=',
            selectAll: '='
        },
        templateUrl: function (element, attrs) {
            return attrs.templateUrl || 'CreateCloud/selectRect.html';
        },
        controller: 'SelectRectController',
        controllerAs: 'rect'

    };
    return directive;
}


function ubtnGroupHandle() {
    return {
        onTabClick: function () {}
    }
}

function selectRectHandle() {
    return {
        remove: function () {}
    }
}

function uuid() {
    return {
        create: function () {
            var len = 32; //32长度
            var radix = 16; //16进制
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [],
                i;
            radix = radix || chars.length;
            if (len) {
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
            } else {
                var r;
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random() * 16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }
            return uuid.join('');
        }
    }
}

function SelectRectController($scope, selectRectHandle) {
    var self = this;
    var index = self.index = $scope.index;
    console.log('index--->', index);
    var options = self.options = $scope.options;
    var selectAll = $scope.selectAll;
    var selected = self.selected = selectAll[index];

    //self.selected = selectAll[index];
    self.onChange = function () {
        selectAll[index] = selected;
        console.log('--->%s,%s', index, self.selected);
    }

    self.close = function () {
        selectRectHandle.remove(index);
        //selectAll.splice(index,1);
        console.log('SelectRectController-------->%o,%o', index, selectAll[index]);
        console.log('close-------->%o', selectAll);
    }

}

function CreateCloudController($scope, selectRectHandle, ubtnGroupHandle, uuid, ISP) {
    var self = this;
    self.model = {};
    self.model.uhostLocals = {
        ubtnsIndex: 0,
        ubtns: [{
            label: '上海'
        }, {
            label: '重庆'
        }],
        usageSelected: {
            label: '罗泾',
            code: '01'
        },
        usageOptions: [{
            label: '罗泾',
            code: '01'
        }, {
            label: '张江',
            code: '02'
        }]
    };
    self.model.uhostNetwork = {
        ubtnsIndex: 1,
        ubtns: [{
            label: '暂不需要'
        }, {
            label: '需要'
        }],
        vpcSelected: {
            label: 'VPC-01',
            code: '01'
        },
        vpcOptions: [{
            label: 'VPC-01',
            code: '01'
        }, {
            label: 'VPC-02',
            code: '02'
        }],
        subnetSelected: {
            label: 'VPC-01',
            code: '01'
        },
        subnetOptions: [{
            label: '10.25.74.0/15',
            code: '01'
        }, {
            label: '10.25.78.0/15',
            code: '02'
        }]
    };


    var bandwidthDataSource = [{
        "ciCode": "PBWTDC0001",
        "customerId": "14",
        "ciName": "SSLVPN因特网移动线路带宽(PBWTDC0001)",
        "netOperator": "移动",
        "ciId": "13932",
        "bandwidthType": "独占带宽"
    }, {
        "ciCode": "PBWTDC0002",
        "customerId": "14",
        "ciName": "SSLVPN因特网联通线路带宽(PBWTDC0002)",
        "netOperator": "联通",
        "ciId": "13933",
        "bandwidthType": "独占带宽"
    }, {
        "ciCode": "PBWTDC0003",
        "customerId": "14",
        "ciName": "SSLVPN因特网电信线路带宽(PBWTDC0003)",
        "netOperator": "电信",
        "ciId": "13934",
        "bandwidthType": "独占带宽"
    }, {
        "ciCode": "PBWT320001",
        "customerId": "14",
        "ciName": "因特网网络带宽(PBWT320001)",
        "netOperator": "联通",
        "ciId": "20670",
        "bandwidthType": "独占带宽"
    }, {
        "ciCode": "PBWT320002",
        "customerId": "14",
        "ciName": "因特网网络带宽(PBWT320002)",
        "netOperator": "联通",
        "ciId": "22073",
        "bandwidthType": "独占带宽"
    }]
    //test2
    var bandwidthType = [{
        label: '独占带宽',
        code: '01',
        netOperators: [{
            label: '联通',
            code: '01',
            bandwidths: [{
                ciCode: 'PBWT320001',
                ciName: "因特网网络带宽(PBWT320001)"
            }, {
                ciCode: 'PBWT320002',
                ciName: "因特网网络带宽(PBWT320002)"
            }]
        }, {
            label: '电信',
            code: '02',
            bandwidths: [{
                ciCode: 'PBWT320011',
                ciName: "因特网网络带宽(PBWT320011)"
            }, {
                ciCode: 'PBWT320012',
                ciName: "因特网网络带宽(PBWT320012)"
            }]
        }, {
            label: '移动',
            code: '03',
            bandwidths: [{
                ciCode: 'PBWT320021',
                ciName: "因特网网络带宽(PBWT320021)"
            }, {
                ciCode: 'PBWT320022',
                ciName: "因特网网络带宽(PBWT320022)"
            }]
        }]
    }, {
        label: '共享带宽',
        code: '02',
        netOperators: [{
            label: '联通',
            code: '01',
            bandwidths: []
        }, {
            label: '电信',
            code: '02',
            bandwidths: []
        }, {
            label: '移动',
            code: '03',
            bandwidths: []
        }]
    }];

    //self.uhostBandwidth = {};
    self.uhostBandwidth = $scope.$new();
    self.uhostBandwidth.datasets = {
        0: [{
            label: '独占带宽',
            code: '1'
        }, {
            label: '共享带宽',
            code: '2'
        }],
        1: [{
            label: '电信',
            code: '1',
            bandwidth: '1'
        }, {
            label: '联通',
            code: '2',
            bandwidth: '1'
        }, {
            label: '移动',
            code: '3',
            bandwidth: '1'
        }, {
            label: '电信',
            code: '4',
            bandwidth: '2'
        }, {
            label: '联通',
            code: '5',
            bandwidth: '2'
        }],
        2: [{
            ciCode: 'PBWT320021',
            ciId: '13934',
            ciName: '电信因特网网络带宽(PBWT320021)',
            netOperator: '1'
        }, {
            ciCode: 'PBWT320022',
            ciId: '13935',
            ciName: '联通因特网网络带宽(PBWT320022)',
            netOperator: '2'
        }, {
            ciCode: 'PBWT320023',
            ciId: '13936',
            ciName: '因特网网络带宽(PBWT320023)',
            netOperator: '3'
        }, {
            ciCode: 'PBWT320024',
            ciId: '13937',
            ciName: '因特网网络带宽(PBWT320024)',
            netOperator: '1'
        }],
        selected:['1','1','13934']
    };

    // var netOperators = _.uniq(_.pluck(bandwidthDataSource, 'netOperator'));
    // console.log('bandwidthDataSets--->', self.uhostBandwidth.datasets[0]);

    self.uhostBandwidth.getISP = function(){
        //console.log('getISP--->',this.datasets.selected[0]);
        return _.where(this.datasets[1], {bandwidth: this.datasets.selected[0] });
    }

    self.uhostBandwidth.getBandwidth = function(){
        return _.where(this.datasets[2], {netOperator: this.datasets.selected[1] });
    }

    self.uhostBandwidth.ISPChange = self.uhostBandwidth.datasets.selected[1];

    self.uhostBandwidth.$watch('ISPChange',function(newVal){
        //console.log('ISPChange-->',newVal);
        self.uhostBandwidth.datasets.selected[1] = newVal;
    });

    self.uhostBandwidth.bandwidthChange = function(){
        console.log('bandwidthChange--->');
    }

    self.model.uhostISO = {
        isoSelected: {
            code: '01'
        },
        isoOptions: [{
            label: 'Window Server 2008',
            code: '01'
        }, {
            label: 'RedHat',
            code: '02'
        }]
    }

    self.model.uhostStorage = {}
        // self.model.uhostStorage = {
        //     storageSelected: {code:'100'},
        //     storageOptions: [{label:'100',code:'100'},{label:'200',code:'200'}]
        // }

    self.model.uhostStorage.selected = [];
    self.model.uhostStorage.options = [{
        label: '100',
        code: '100'
    }, {
        label: '200',
        code: '200'
    }];

    //////////////////////////////////////////////////
    self.storageAdd = function () {
        self.model.uhostStorage.selected.push({
            label: '100',
            code: '100',
            _id: uuid.create()
        });
        console.log(self.model.uhostStorage.selected);
    }
    selectRectHandle.remove = function (index) {
        self.model.uhostStorage.selected.splice(index, 1);
        console.log('storageRemove--->', index);
    }
    self.submit = function () {
        console.log(self.model.uhostStorage.selected);
    }


}